package edu.fasta.sisinfo2.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import edu.fasta.sisinfo2.net.DatosUsuario;
import edu.fasta.sisinfo2.net.InterfazComandoServer;
import edu.fasta.sisinfo2.net.NewUserVerify;
import edu.fasta.sisinfo2.net.RecargaFichas;
import edu.fasta.sisinfo2.poker.PokerControllerCliente;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;

public class RecargarFichas extends JDialog {

	private final JPanel contentPanel = new JPanelFondo("/Img/Login2.png",0,0);
	private JTextField cantidadFichas;
	private JTextField confirmField;
	private JButton okButton = new JButton();
	private JButton cancelButton = new JButton();
	private PokerControllerCliente PCC;
	private InterfazComandoServer comando;
	private int ciegaGrande = -1;
	private Icon r1 = new ImageIcon(getClass().getResource( "/Img/recargar1.png" ));
	private Icon r2 = new ImageIcon(getClass().getResource( "/Img/recargar2.png" ));
	private Icon r3 = new ImageIcon(getClass().getResource( "/Img/recargar3.png" ));
	private Icon s1 = new ImageIcon(getClass().getResource( "/Img/Salir1.png" ));
	private Icon s2 = new ImageIcon(getClass().getResource( "/Img/Salir2.png" ));
	private Icon s3 = new ImageIcon(getClass().getResource( "/Img/Salir3.png" ));

	/**
	 * Create the dialog.
	 */
	public RecargarFichas(PokerControllerCliente unPCC, int ciegaG) {
		this.setPCC(unPCC);
		setTitle("Texas UFasta - Recarga de Fichas");
		setBounds(100, 100, 321, 218);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.setCiegaGrande(ciegaG);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNombreDeUsuario = new JLabel("Cantidad");
			lblNombreDeUsuario.setBounds(58, 41, 69, 20);
			contentPanel.add(lblNombreDeUsuario);
		}
		{
			cantidadFichas = new JTextField();
			cantidadFichas.setText("200");
			cantidadFichas.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e)
				   {
				      char caracter = e.getKeyChar();

				      // Verificar si la tecla pulsada no es un digito
				      if(((caracter < '0') ||
				         (caracter > '9')) &&
				         (caracter != '\b' /*corresponde a BACK_SPACE*/))
				         
				      {
				         e.consume();  // ignorar el evento de teclado
				      }
				   }
				@Override
				public void keyReleased(KeyEvent arg0) {
					if(cantidadFichas.getText().equals(confirmField.getText()) &&
							(Integer.parseInt(cantidadFichas.getText()) >= getCiegaGrande())){
						okButton.setEnabled(true);						
					}else{
						okButton.setEnabled(false);
					}
				}
			});
			cantidadFichas.setBounds(125, 41, 173, 20);
			contentPanel.add(cantidadFichas);
			cantidadFichas.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Confirmar");
			lblPassword.setBounds(58, 73, 69, 20);
			contentPanel.add(lblPassword);
		}
		{
			confirmField = new JTextField();
			confirmField.setBounds(125, 73, 173, 20);
			confirmField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					if(cantidadFichas.getText().equals(confirmField.getText()) &&
							(Integer.parseInt(confirmField.getText()) >= getCiegaGrande())){
						okButton.setEnabled(true);						
					}else{
						okButton.setEnabled(false);
					}
				}
				@Override
				public void keyTyped(KeyEvent e)
				   {
				      char caracter = e.getKeyChar();

				      // Verificar si la tecla pulsada no es un digito
				      if(((caracter < '0') ||
				         (caracter > '9')) &&
				         (caracter != '\b' /*corresponde a BACK_SPACE*/))
				         
				      {
				         e.consume();  // ignorar el evento de teclado
				      }
				   }			
			});
			contentPanel.add(confirmField);
		}
		
		{
			JLabel lblCreacionDeNuevo = new JLabel("Recarga de fichas.");
			lblCreacionDeNuevo.setForeground(Color.WHITE);
			lblCreacionDeNuevo.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblCreacionDeNuevo.setBounds(107, 8, 121, 22);
			contentPanel.add(lblCreacionDeNuevo);
		}
		{
			okButton = new JButton( "", r1 );
			okButton.setEnabled(false);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getPCC().enviarComando(new RecargaFichas(getPCC().getNombreJugador(), Integer.parseInt(cantidadFichas.getText())));
					dispose();
				}
			});
			okButton.setRolloverIcon( r2 );
			okButton.setPressedIcon(r3);
			okButton.setBorder(BorderFactory.createEmptyBorder());
			okButton.setContentAreaFilled(false);
			okButton.setBounds(43, 110, 114, 39);
			contentPanel.add(okButton);
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			cancelButton = new JButton( "", s1 );
			cancelButton.setRolloverIcon( s2 );
			cancelButton.setPressedIcon(s3);
			cancelButton.setBorder(BorderFactory.createEmptyBorder());
			cancelButton.setContentAreaFilled(false);
			cancelButton.setBounds(173, 110, 114, 39);
			contentPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			cancelButton.setActionCommand("Cancel");
		}
		this.setVisible(true);
	}
	

	public PokerControllerCliente getPCC() {
		return PCC;
	}

	public void setPCC(PokerControllerCliente pCC) {
		PCC = pCC;
	}

	public JTextField getTextField() {
		return cantidadFichas;
	}

	public void setTextField(JTextField textField) {
		this.cantidadFichas = textField;
	}

	public JTextField getPasswordField() {
		return confirmField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.confirmField = passwordField;
	}

	public InterfazComandoServer getComando() {
		return comando;
	}

	public void setComando(InterfazComandoServer comando) {
		this.comando = comando;
	}


	public int getCiegaGrande() {
		return ciegaGrande;
	}


	public void setCiegaGrande(int ciegaGrande) {
		this.ciegaGrande = ciegaGrande;
	}
	
}
